// Displays a list of Acsii chars in Decimal, Oct, Hex, Binary and the sysmbol and HTML codes
// Chars below 31 print there names.
// By DreamVB
// 23:58 5/6/2016

#include <iostream>
#include <iomanip>
using namespace std;
using std::cout;
using std::endl;
char *Dec2Bin(int dec){
	char TempBin[32];
	int j = 0;
	int i = 30;
	int t = dec;
	int bitcnt = 0;
	bitcnt = 7;
	while(i >=0)
	{
		//Build the binary number.
		if((dec & (1 << i)) != 0){
			TempBin[j] = '1';
		}else{
			TempBin[j] = '0';
		} 
		//INC Counters
		i--;
		j++;
}
	
	//Reserve array contents
	for(i=bitcnt;i>=0;i--){
		//Reserve array chars
		TempBin[bitcnt-i] = TempBin[30-i];
	}
	//Convert char array to Char*
	i = strlen(TempBin);
	//Resize array to hold the data
	char *pBin =(char*)malloc(i+1*sizeof(char));
	//Copy from TempBin to pStr
	strcpy(pBin,TempBin);
	//Set ending
	pBin[bitcnt+1] = '\0';
	//Clear up
	memset(TempBin,NULL,sizeof(TempBin));
	//Return
	return pBin;
}
std::string Hex2(int number)
{
 char buffer[5]; // assume 32 bit && sizeof(int) == 4
 sprintf(buffer, "%02X", number);
 return std::string(buffer, buffer+5);
}
std::string Int2(int number)
{
 char buffer[5]; // assume 32 bit && sizeof(int) == 4
 sprintf(buffer, "%03d", number);
 return std::string(buffer, buffer+5);
}
std::string Oct2(int number)
{
 char buffer[5]; // assume 32 bit && sizeof(int) == 4
 sprintf(buffer, "%03o", number);
 return std::string(buffer, buffer+5);
}
void main(){
	int i = 0;
	string s0 = "";
	char *sBin = "";
	unsigned c = '\0';
	system("title ASCII Table");
	//All values below 32 names
	char *NonePrint[] = {"NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL","BS",
	"HT","LF","VT","FF","CR","SO","SI","DLE","DC1","DC2","DC3","DC4","NAK","SYN",
	"ETB","CAN","EM","SUB","ESC","FS","GS","RS","US"};
	
	//Set headers
	cout << "DEC\tOCT\tHEX\tBIN\t\tSymbol\tHTML" << endl;
	do{
		//Convert decimal to bin
		sBin = Dec2Bin(i);
		if(i <=31){
			//Print special name
			s0 = NonePrint[i];
		}else{
			//Print chars
			s0 = (unsigned char)i;
		}
		cout << i << "\t" << Oct2(i).c_str() <<
			"\t" << Hex2(i).c_str() <<
			"\t" << sBin << 
			"\t" << s0.c_str() << 
			"\t" << "&#" << Int2(i).c_str() << ";" << endl;
		free(sBin);
		//INC counter
		i++;
	}while(i <=255);
	//Clear up
	s0.clear();
	//Pause console
	system("pause");
}